<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="90%" destroy-on-close @closed="$emit('closed')" :align-center="true">
        <el-form :model="form" :rules="rules" ref="dialogForm" label-width="110px" label-position="top" style="height:450px;overflow:auto">
            <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete>
                <el-table-column prop="factory_number" label="工厂货号" align="center"></el-table-column>
                <el-table-column prop="batch_number" label="批次号" align="center"></el-table-column>
                <el-table-column prop="colour" label="颜色" align="center"></el-table-column>
                <el-table-column prop="orderDetail" label="尺码" align="center">
                    <template #default="scope">
                        <div v-for="(item,index) in scope.row.orderDetail" :key="index" style="margin:5px 0">
                            <el-input v-model="item.goods_size" readonly></el-input>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="orderDetail" label="收货数量" align="center">
                    <template #default="scope">
                        <div v-for="(item,index) in scope.row.orderDetail" :key="index" style="margin:5px 0">
                            <el-input v-model="item.num" readonly></el-input>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="put_num" label="入库数量" align="center">
                    <template #default="scope">
                        <div v-for="(item,index) in scope.row.orderDetail" :key="index" style="margin:5px 0">
                            <el-input v-model="item.put_num" placeholder="请输入入库数量"></el-input>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="warehouse_id" label="入库仓库" align="center">
                    <template #default="scope">
                        <div v-for="(item,index) in scope.row.orderDetail" :key="index" style="margin:5px 0">
                            <el-cascader v-model="item.warehouse_id" :options="wareList" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" width="150">
                    <template #default="scope">
                        <div v-for="(item,index) in scope.row.orderDetail" :key="index" style="margin:5px 0">
                            <el-input v-model="item.remark" placeholder="请输入备注"></el-input>
                        </div>
                    </template>
                </el-table-column>
            </sc-form-table>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">完 成</el-button>
		</template>
	</el-dialog>
</template>
<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '成品入库',
					edit: '成品入库',
					over: '成品入库'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    contract_no:"",
                    detail:[],
				},
				//验证规则
				rules: {
                    audit_status: [
						{required: true, message: '请选择'}
					],
				},
                wareList:[],
                groupsProps: {
                    value: "id",
                    emitPath: false,
                    checkStrictly: true
                }
			}
		},
		mounted() {
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						res = await this.$API.produce.send.putHouse.post(this.form);
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
                this.form.contract_no = data.contract_no
                let a = []
                data.orderDetail.map(item=>{
                    a.push({
                        goods_size:item.goods_size,
                        num:item.num,
                        put_num:"",
                        warehouse_id:"",
                        remark:"",
                    })
                })
                let array = []
                    array.push({
                        goods_id:data.goods_id,
                        factory_number:data.contract.factory_number,
                        batch_number:data.contract.batch_number,
                        colour:data.goods.colour,
                        orderDetail:a,
                        unit:data.goods.unit_id,
                        supplier_id:data.contract.supplier_id,
                        order_no:data.contract.order_no,
                })
                this.form.detail = array
			},
            // 获取仓库
            async getWare(){
                var res = await this.$API.all.ware.get();
                this.wareList = res.data;
            },
		}
	}
</script>

<style lang="scss">
.el-form-item--default{
    align-items: center;
}
</style>
